package com.cis.commom;

import java.util.HashMap;
import java.util.Map;


/**
 * 用于返回值封装，也用于服务端与客户端的 json 数据通信
 * 响应封装：：重新实现Ret。
 * 1 state和code两个字段等同定义：state==ok,code=0;非零失败！
 *
 * <pre>
 * 一、主要应用场景：
 * 1：业务层需要返回多个返回值，例如要返回业务状态以及数据
 * 2：renderJson(ret) 响应 json 数据给客户端
 *
 *
 * 三、实例
 * 1：服务端
 *    Response ret = service.justDoIt(paras);
 *    renderJson(ret);
 *
 * 2：javascript 客户端 ajax 回调函数通常这么用：
 *    success: function(ret) {
 *       if(ret.state == "ok") {
 *       	...
 *       }
 *
 *       if (ret.state == "fail") {
 *       	...
 *       }
 *    }
 *
 * 3：普通应用程序通常这么用：
 *   String json = HttpKit.readData(getRequest());
 *   Response ret = FastJson.getJson().parse(json, Ret.class);
 *   if (ret.isOk()) {
 *   	...
 *   }
 *
 *   if (ret.isFail()) {
 *   	...
 *   }
 *
 * </pre>
 */
@SuppressWarnings({"rawtypes", "unchecked"})
public final class Response extends HashMap {

	private static final String STATE = "state";
	private static final String STATE_OK = "ok";
	private static final String STATE_FAIL = "fail";

	private static final String CODE = "code";
	private static final int CODE_OK = 0;
	private static final int CODE_FAIL = -1;//-1是默认错误

	public static void main(String[] args) {
		Response.fail("test");
	}

	public Response() {
	}

	/**
	 * @param code 0 是成功，非0失败
	 */
	public Response setCode(int code) {
		if (code == CODE_OK) {
			setOk();
		} else {
			super.put(STATE, STATE_FAIL);
			super.put(CODE, code);
		}
		return this;
	}

	public Response setOk() {
		super.put(STATE, STATE_OK);
		super.put(CODE, CODE_OK);
		return this;
	}

	public Response setOk(String msg) {
		this.setOk();
		super.put("msg", msg);
		return this;
	}

	public Response setFail() {
		super.put(STATE, STATE_FAIL);
		super.put(CODE, CODE_FAIL);
		return this;
	}

	public Response setFail(String msg) {
		this.setFail();
		super.put("msg", msg);
		return this;
	}

	public boolean isOk() {
		Object state = get(STATE);
		if (STATE_OK.equals(state)) {
			return true;
		}
		if (STATE_FAIL.equals(state)) {
			return false;
		}

		throw new IllegalStateException("调用 isOk() 之前，必须先调用 ok()、fail() 或者 setOk()、setFail() 方法");
	}

	public boolean isFail() {
		Object state = get(STATE);
		if (STATE_FAIL.equals(state)) {
			return true;
		}
		if (STATE_OK.equals(state)) {
			return false;
		}

		throw new IllegalStateException("调用 isFail() 之前，必须先调用 ok()、fail() 或者 setOk()、setFail() 方法");
	}

	public static Response by(Object key, Object value) {
		return new Response().set(key, value);
	}

	public static Response create(Object key, Object value) {
		return new Response().set(key, value);
	}

	public static Response create() {
		return new Response();
	}

	public static Response ok() {
		return new Response().setOk();
	}

	public static Response ok(String msg) {
		return new Response().setOk().set("msg", msg);
	}

	public static Response ok(Object key, Object value) {
		return ok().set(key, value);
	}

	public static Response fail() {
		return new Response().setFail();
	}

	public static Response fail(String msg) {
		return new Response().setFail().set("msg", msg);
	}

	public static Response fail(int code, String msg) {
		if (code == CODE_OK) {
			throw new IllegalStateException("Response.fail参数code必须非0.（0表示成功）");
		}
		return new Response().setFail().set("msg", msg).set(CODE, code);
	}

	public static Response fail(Object key, Object value) {
		return fail().set(key, value);
	}

	public Response set(Object key, Object value) {
		super.put(key, value);
		return this;
	}

	public Response set(Map map) {
		super.putAll(map);
		return this;
	}

	public Response set(Response ret) {
		super.putAll(ret);
		return this;
	}

	public Response delete(Object key) {
		super.remove(key);
		return this;
	}

	public <T> T getAs(Object key) {
		return (T) get(key);
	}

	public String getStr(Object key) {
		Object s = get(key);
		return s != null ? s.toString() : null;
	}

	public Integer getInt(Object key) {
		Number n = (Number) get(key);
		return n != null ? n.intValue() : null;
	}

	public Long getLong(Object key) {
		Number n = (Number) get(key);
		return n != null ? n.longValue() : null;
	}

	public Number getNumber(Object key) {
		return (Number) get(key);
	}

	public Boolean getBoolean(Object key) {
		return (Boolean) get(key);
	}

	/**
	 * key 存在，并且 value 不为 null
	 */
	public boolean notNull(Object key) {
		return get(key) != null;
	}

	/**
	 * key 不存在，或者 key 存在但 value 为null
	 */
	public boolean isNull(Object key) {
		return get(key) == null;
	}

	/**
	 * key 存在，并且 value 为 true，则返回 true
	 */
	public boolean isTrue(Object key) {
		Object value = get(key);
		return (value instanceof Boolean && ((Boolean) value == true));
	}

	/**
	 * key 存在，并且 value 为 false，则返回 true
	 */
	public boolean isFalse(Object key) {
		Object value = get(key);
		return (value instanceof Boolean && ((Boolean) value == false));
	}

	// public String toJson() {
	// 	return Json.getJson().toJson(this);
	// }

	public boolean equals(Object ret) {
		return ret instanceof Response && super.equals(ret);
	}
}